package com.shark.uh.annotation.validator;


import com.shark.uh.annotation.EnumCheck;
import com.shark.uh.utils.ValidationUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class EnumCheckValidator implements ConstraintValidator<EnumCheck, String> {

    private Class<? extends Enum> enumClass;

    @Override
    public void initialize(EnumCheck constraintAnnotation) {
        this.enumClass = constraintAnnotation.enumClass();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (s == null || StringUtils.isBlank(s)) {
            return false;
        }
        return ValidationUtils.isEnumName(s, enumClass);
    }
}
